/**
 * 
 */
package com.gr.staffpm.tasks.model;

import org.apache.wicket.model.LoadableDetachableModel;

import com.gr.staffpm.datatypes.TaskQueue;
import com.gr.staffpm.tasks.service.TaskQueueService;


/**
 * @author Graham Rhodes 16 Jan 2011 16:35:23
 */
public class DetachableTaskQueueModel extends LoadableDetachableModel<TaskQueue> {

    private final int id;
    private final TaskQueueService taskQueueService;
    private static final long serialVersionUID = 1L;

    public DetachableTaskQueueModel(TaskQueueService tqs, TaskQueue taskQueue) {
        this.taskQueueService = tqs;
        this.id = taskQueue.getQueueId();
    }

    @Override
    public int hashCode() {
        return Long.valueOf(id).hashCode();
    }

    /**
     * Used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     * 
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (obj instanceof DetachableTaskQueueModel) {
            DetachableTaskQueueModel other = (DetachableTaskQueueModel) obj;
            return other.id == id;
        }
        return false;
    }


    @Override
    protected TaskQueue load() {
        return taskQueueService.getQueuedTask(id);
    }

}
